﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PreeRegisterModel;
using System.Data;
using PreeRegisterDAL;
using DrSoftsFrame;

namespace PreeRegisterBLL
{
    public class WorkGetBLL
    {
        /// <summary>
        /// 排班号源获取
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static WorkGetResponseBodyModel GetWork(WorkGetRequestBodyModel request)
        {
            try
            {
                //查询排班号源
                WorkGetResponseBodyModel workGetResponseBodyModel = new PreeRegisterModel.WorkGetResponseBodyModel();

                if (request.docid.Trim() == "" && request.deptid.Trim() == "")
                {
                    return null;
                }
                if (request.preetype.Trim() == "")
                {
                    return null;
                }

                int iPreeDay = WorkGetDAL.GetPreeDay(Common.ToInt(request.preetype));

                DataSet ds = WorkGetDAL.GetWork(Common.ToInt(request.docid), Common.ToInt(request.deptid), iPreeDay, VariableBLL.IFTmkyy, Common.ToInt(request.preetype));

                //赋值排班数量实体

                List<WorkGetRespModel> works = new List<WorkGetRespModel>();
                foreach (DataRow drWorkPlan in ds.Tables["WorkPlan"].Rows)
                {
                    string iWorkPlanID = drWorkPlan["iWorkPlanID"].ToString();

                    WorkGetRespModel work = new WorkGetRespModel();

                    work.wid = iWorkPlanID;
                    work.date = drWorkPlan["dDate"].ToString();
                    work.mcount = drWorkPlan["nMaxPreengageNum"].ToString();
                    work.tcount = drWorkPlan["nMaxDiagnoseNum"].ToString();
                    work.acount = drWorkPlan["leftpreenum"].ToString();
                    work.pcode = drWorkPlan["cTimeTypeCode"].ToString();
                    work.stime = drWorkPlan["stime"].ToString();
                    work.etime = drWorkPlan["etime"].ToString();
                    work.rankid = drWorkPlan["cDiagnoseTypeCode"].ToString();
                    work.wrank = drWorkPlan["cDiagnoseTypeName"].ToString();
                    work.price = drWorkPlan["registermoney"].ToString();
                    work.ofee = drWorkPlan["nDiagnoseRate"].ToString();
                    //刘鹏林   2015-11-23  增加返回参数加收费
                    work.addrate = drWorkPlan["nAddRate"].ToString();
                    work.addr = drWorkPlan["cLocation"].ToString();
                    work.preeflag = drWorkPlan["bPreeFlag"].ToString();
                    work.doctorname = drWorkPlan["doctorname"].ToString();
                    work.doctorid = drWorkPlan["doctorid"].ToString();

                    //去掉号源赋值  liujia151214
                    ////获取号源信息
                    //if (VariableBLL.bPreTime)
                    //{
                    //    DataView dvWorkPlanDetail = new DataView(ds.Tables["WorkPlanDetail"]);
                    //    dvWorkPlanDetail.RowFilter = "iWorkPlanID=" + iWorkPlanID;

                    //    List<WorkGetDetail> workDetails = new List<WorkGetDetail>();
                    //    foreach (DataRowView drvDetail in dvWorkPlanDetail)
                    //    {

                    //        WorkGetDetail workDetail = new WorkGetDetail();

                    //        workDetail.id = drvDetail["iDetailId"].ToString();
                    //        workDetail.inum = drvDetail["iNum"].ToString();
                    //        workDetail.time = drvDetail["stime"].ToString();
                    //        workDetail.state = drvDetail["istatus"].ToString();

                    //        workDetails.Add(workDetail);
                    //    }

                    //    //work.no = workDetails;
                    //}

                    works.Add(work);
                }

                workGetResponseBodyModel.works = works;

                //返回
                return workGetResponseBodyModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }


        /// <summary>
        /// 号源获取
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static WorkSourceResponseModel GetWorkSource(WorkSourceRequestModel request)
        {
            try
            {
                //查询号源
                WorkSourceResponseModel workSourceResponseModel = new PreeRegisterModel.WorkSourceResponseModel();
                DataSet ds;
                //如果排班ID不为空则直接拿排班ID获取号源，排班ID为空则先拿医生id、就诊时间、时段（上午、下午）来获取排班ID 
                if (request.wid.Trim() == "")
                {
                    if (request.doctorid.Trim() == "" || request.date.Trim() == "" || request.time.Trim() == "")
                    {
                        return null;
                    }
                    else
                    {
                        //用医生id、就诊时间、时段（上午、下午）来获取排班ID 
                        string wid = WorkGetDAL.GetWorkPlanID(request.doctorid.Trim(), request.date.Trim(), request.time.Trim());
                        if (wid.Trim() == "")
                            return null;
                        ds = WorkGetDAL.GetWorkDetail(wid.Trim(), VariableBLL.IFTmkyy);
                    }
                }
                else
                {
                     ds = WorkGetDAL.GetWorkDetail(request.wid.Trim(), VariableBLL.IFTmkyy);
                }

                

                //赋值

                List<WorkGetDetail> detail_list = new List<WorkGetDetail>();
                foreach (DataRow drSource in ds.Tables[0].Rows)
                {
                    WorkGetDetail detail = new WorkGetDetail();

                    detail.id = drSource["iDetailId"].ToString();
                    detail.inum = drSource["iNum"].ToString();
                    detail.time = drSource["stime"].ToString();
                    detail.state = drSource["istatus"].ToString();
                    detail.wid = drSource["iWorkPlanID"].ToString();
                    detail_list.Add(detail);
                }

                workSourceResponseModel.no = detail_list;

                //返回
                return workSourceResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }
    }
}
